<?php

namespace app\admin\controller;

use app\common\controller\Backend;

class Admin extends Backend
{

    public function adminlist()
    {
        $where = null;
        $adminList = null;
        if ($this->request->isGet()) {
            $params = $this->request->param();
            $name = !empty($params['search_field']) ? $params['search_field'] : '';
            $keyword = !empty($params['keyword']) ? $params['keyword'] : '';

            if ($name == 'username') {
                $where = "1";
                $adminList = model('Admin')->where('username', 'like', "%$keyword%")->paginate(7);
                // echo model('Admin')->getlastsql();
                // exit;
            }
            if ($name == 'nickname') {
                $where = "1";
                $adminList = model('Admin')->where('nickname', 'like', "%$keyword%")->paginate(7);
            }
            if ($where == null) {
                $adminList = model('Admin')->paginate(7);
            }
        }

        $navTitle = '管理员管理';
        $navSmall = '管理员列表';
        return $this->fetch('', [
            'navTitle' => $navTitle,
            'navSmall' => $navSmall,
            'adminList' => $adminList
        ]);
    }

    public function adminState()
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            $params = $this->request->param();
            $result = null;
            if ($params['action'] == 'checkboxDisabled') {
                $data = [];
                foreach ($params['list'] as $key => $item) {
                    $data[$key]['id'] = $params['listId'][$key];
                    $data[$key]['state'] = $params['list'][$key] = 0;
                }
                $result = model('Admin')->isUpdate(true)->saveAll($data);
                if ($result === FALSE) {
                    $this->error(model('Admin')->getError());
                } else {
                    $this->success('禁用成功', url('admin/admin/adminlist'));
                }
            }
            if ($params['action'] == 'checkboxEnable') {
                $data = [];
                foreach ($params['list'] as $key => $item) {
                    $data[$key]['id'] = $params['listId'][$key];
                    $data[$key]['state'] = $params['list'][$key] = 1;
                }
                $result = model('Admin')->isUpdate(true)->saveAll($data);
                if ($result === FALSE) {
                    $this->error(model('Admin')->getError());
                } else {
                    $this->success('启用成功', url('admin/admin/adminlist'));
                }
            }
        }
    }

    public function adminadd()
    {
        if ($this->request->isPost()) {
            $action = $this->request->param('action', '');
            //ajax检查名称
            if ($action == 'username') {
                $resName = model('Admin')->where('username', $this->request->param('username'))->find();
                if ($resName) {
                    $this->error('用户名称已存在');
                } else {
                    $this->success('用户名称可以使用');
                }
            }

            //ajax检查昵称
            if ($action == 'nickname') {
                $resName = model('Admin')->where('nickname', $this->request->param('nickname'))->find();
                if ($resName) {
                    $this->error('用户昵称已存在');
                } else {
                    $this->success('用户昵称可以使用');
                }
            }

            $params = $this->request->param();
            $resUsername = model('Admin')->where('username', $this->request->param('username'))->find();
            if ($resUsername) {
                $this->error('用户名称已存在');
            }
            $resNickname = model('Admin')->where('nickname', $this->request->param('nickname'))->find();
            if ($resNickname) {
                $this->error('用户昵称已存在');
            }

            if ($params['password'] != $params['repassword']) {
                $this->error('密码与确认密码不一样');
            }

            // 生成密码盐
            $salt = build_ranstr();

            // 进行加密
            $password = md5($params['password'] . $salt);
            $adminData = [
                'username' => $params['username'],
                'nickname' => $params['nickname'],
                'password' => $password,
                'salt' => $salt
            ];

            $result = model('Admin')->validate('common/Admin/Admin')->save($adminData);
            if ($result === FALSE) {
                $this->error(model('Admin')->getError());
            } else {
                $this->success('新增成功', url('admin/admin/adminlist'));
            }
        }
        $navTitle = '管理员管理';
        $navSmall = '管理员列表';
        return $this->fetch('', [
            'navTitle' => $navTitle,
            'navSmall' => $navSmall
        ]);
    }

    public function admindel()
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            $action = $this->request->param('action', '');
            $result = null;
            if ($action == 'del') {
                $id = $this->request->param('id', 0);
                $result = model('Admin')->where('id', $id)->delete();
            }
            if ($action == 'checkboxRemove') {
                $list = $this->request->param();
                // halt($list['list']);
                $result = model('Admin')->destroy($list['list']);
            }

            if ($result === FALSE) {
                $this->error(model('Admin')->getError());
            } else {
                $this->success('删除成功');
            }
        }
    }

    public function adminedit()
    {
        $id = $this->request->param('id', 0);
        $adminList = model('Admin')->find($id);
        if ($this->request->isPost()) {
            $params = $this->request->param();
            // halt($params);
            if ($adminList['username'] != $params['username']) {
                $resUsername = model('Admin')->where('username', $this->request->param('username'))->find();
                if ($resUsername) {
                    $this->error('用户名称已存在');
                }
            }

            if ($adminList['nickname'] != $params['nickname']) {
                $resNickname = model('Admin')->where('nickname', $this->request->param('nickname'))->find();
                if ($resNickname) {
                    $this->error('用户昵称已存在');
                }
            }

            $password = '';
            $salt = '';
            if (!empty($params['oripassword']) && !empty($params['password']) && !empty($params['repassword'])) {
                // 进行加密
                $pass = md5($params['oripassword'] . $adminList['salt']);
                if ($pass != $adminList['password']) {
                    $this->error('原密码不正确');
                }
                if ($params['password'] != $params['repassword']) {
                    $this->error('密码与确认密码不一样');
                }
                // 生成密码盐
                $salt = build_ranstr();
                $password = md5($params['password'] . $salt);
            } else {
                $salt = $adminList['salt'];
                $password = $adminList['password'];
            }

            $updateData = [
                'id' => $params['id'],
                'username' => $params['username'],
                'nickname' => $params['nickname'],
                'password' => $password,
                'state' => $params['state'],
                'salt' => $salt
            ];

            $result = model('Admin')->isUpdate(true)->validate('common/Admin/Admin')->save($updateData);
            if ($result === FALSE) {
                $this->error(model('Admin')->getError());
            } else {
                $this->success('编辑成功', url('admin/admin/adminlist'));
            }
        }
        $navTitle = '管理员管理';
        $navSmall = '管理员列表';
        return $this->fetch('', [
            'navTitle' => $navTitle,
            'navSmall' => $navSmall,
            'adminList' => $adminList
        ]);
    }
}
